﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNRegex.Entity;
using CNRegex.Biz.BizAccess;
using CNRegex.Entity.Others;
using System.Linq.Expressions;
using CNRegex.Web.Utility;
using System.Text;

namespace CNRegex.Web.Controllers
{
	public class TestController : Controller
	{

		public ActionResult Index()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Index(SearchTest searchTest)
		{
	
			return View();
		}

		
	}

	public class ComposeSearchExpression<T>
	{
		public BinaryExpression Binarys { get; set; }
		private ParameterExpression pLeft = Expression.Parameter(typeof(T));
		public ComposeSearchExpression<T> ComposeSearchCondition(Expression<Func<T,object>> member, object value)
		{
			MemberExpression memberExpr = null;
			if (member.Body.NodeType == ExpressionType.Convert)
			{
				memberExpr =
					((UnaryExpression)member.Body).Operand as MemberExpression;
			}
			else if (member.Body.NodeType == ExpressionType.MemberAccess)
			{
				memberExpr = member.Body as MemberExpression;
			}


			MemberExpression memberExpressionLeft = Expression.Property(pLeft, memberExpr.Member.Name);
			ConstantExpression right = Expression.Constant(value);
			BinaryExpression binary = Expression.Equal(memberExpressionLeft, right);
			if (Binarys != null)
				Binarys = Expression.MakeBinary(ExpressionType.AndAlso, Binarys, binary);
			else
				Binarys = binary;
			return this;
		}

		public Expression<Func<T, bool>> SearchExpression
		{
			get
			{
				return Expression.Lambda<Func<T, bool>>(Binarys, pLeft);
			}
		}

	}
}
